package gxt.visual.selection;

import gxt.visual.selection.SelectionManager;
import gxt.visual.selection.consumer.ISelectionConsumer;
import gxt.visual.selection.provider.ISingleMixedSelectionProvider;

import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelTag;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;

/**
 * This selection manager manages the selection between a producer that produces BeanModel and a consumer that consumes the actual BEAN <br>
 * @author eugenp
 */
public class SelectionManagerMixed< T extends BeanModelTag > extends SelectionManager< T, T >{
	
	public SelectionManagerMixed(){
		super();
	}
	
	// API
	/**
	 * @param selectionProviderToSet - IS checked for null
	 * @param selectionConsumerToSet - IS checked for null
	 */
	public final void register( final ISingleMixedSelectionProvider< T > selectionProviderToSet, final ISelectionConsumer< T > selectionConsumerToSet ){
		super.register( selectionProviderToSet );
		
		selectionProviderToSet.addSelectionChangedListener( new SelectionChangedListener< BeanModel >(){
			@Override
			public final void selectionChanged( final SelectionChangedEvent< BeanModel > scEv ){
				final BeanModel selectedItem = scEv.getSelectedItem();
				if( selectedItem != null ){
					selectionConsumerToSet.consumeSelection( (T) selectedItem.getBean() );
				}
				else{
					selectionConsumerToSet.consumeSelection( null );
				}
			}
		} );
		
	}
	
}
